﻿using Nasty.Core.Attributes;
using Nasty.Core.Entity;
using Nasty.Core.Session;
using SqlSugar;

namespace Nasty.QuartzModule.QuartzTask
{
    [SugarTable("SysQuartzTask", TableDescription = "定时任务")]
    public class QuartzTask : StandardEntity<QuartzTask>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [NastyColumn(ColumnName = "Name", ColumnDescription = "名称", Required = true)]
        public string? Name { get; set; }

        /// <summary>
        /// 组名称
        /// </summary>
        [NastyColumn(ColumnName = "Group", ColumnDescription = "组名称", Required = true)]
        public string? Group { get; set; }

        /// <summary>
        /// Cron表达式
        /// </summary>
        [NastyColumn(ColumnName = "Cron", ColumnDescription = "Cron表达式", Required = true)]
        public string? Cron { get; set; }

        /// <summary>
        /// Job类名
        /// </summary>
        [NastyColumn(ColumnName = "Class", ColumnDescription = "Job类名", Required = true)]
        public string? Class { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [NastyColumn(ColumnName = "Remark", ColumnDescription = "备注", IsNullable = true)]
        public string? Remark { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [NastyColumn(ColumnName = "IsEnabled", ColumnDescription = "是否启用")]
        public bool IsEnabled { get; set; }



        public override void OnPreAdd()
        {
            base.OnPreAdd();
            this.IsEnabled = false;
        }

        public override void OnPreDelete()
        {
            base.OnPreDelete();
            if (this.IsEnabled) throw new Exception("Job启用中,无法删除");

            var db = AppSession.CurrentDb.Value;
            db.Deleteable<QuartzTaskLog>().Where((t)=>t.TaskId == this.Id).ExecuteCommandAsync();
        }

    }
}
